<?php

namespace Apps\Common\Models;

/**
 * 美容院标签Model
 * @author     chenxiaolin
 * @date        2016年1月19日
 * @version     2.0.0
 * @copyright   Copyright 2016 meelier.com
 */
class BeautyParlorTag extends \PhalconPlus\Database\Model
{

    /**
     *
     * @var integer
     */
    public $bp_tag_id;

    /**
     *
     * @var integer
     */
    public $bp_id;

    /**
     *
     * @var integer
     */
    public $tag_id;

    /**
     *
     * @var string
     */
    public $bp_tag_addtime;
    
    
    public $sort;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->belongsTo('bp_id', 'Apps\Common\Models\BeautyParlor', 'bp_id', array('alias' => 'BeautyParlor'));
        $this->belongsTo('tag_id', 'Apps\Common\Models\BeautyParlorTagInfo', 'tag_id', array('alias' => 'BeautyParlorTagInfo'));
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'beauty_parlor_tag';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return BeautyParlorTag[]
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return BeautyParlorTag
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    /**
     * Independent Column Mapping.
     * Keys are the real names in the table and the values their names in the application
     *
     * @return array
     */
    public function columnMap()
    {
        return array(
            'bp_tag_id' => 'bp_tag_id',
            'bp_id' => 'bp_id',
            'tag_id' => 'tag_id',
            'bp_tag_addtime' => 'bp_tag_addtime',
        	'sort' => 'sort'
        );
    }

    public function defaultColumns()
    {
        return [
            'bp_tag_addtime',
        ];
    }

    /**
     * 获取美容院的标签列表
     * @param int $bp_id 美容院id
     * @return mixed
     */
    public function getTagList($bp_id)
    {
        return self::query()->columns([
            't.tag_title'
        ])->leftJoin('Apps\Common\Models\BeautyParlorTagInfo', 't.tag_id = Apps\Common\Models\BeautyParlorTag.tag_id', 't')->where('t.tag_state = 1 and bp_id = ' . $bp_id)->execute()->toArray();
    }
}
